﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Acsys.Entity;

namespace Acsys
{
    public partial class LoginForm : Form
    {
        User m_user = new User();
        IMainForm m_mainForm;
        bool m_locked = false;

        public LoginForm(IMainForm mainform, bool locked)
        {
            InitializeComponent();
            m_mainForm = mainform;
            m_user = mainform.CurrentUser;
            m_locked = locked;
            if (locked)
            {
                this.Text = "System is Locked";
                Form mf = (Form)mainform;
                mf.Visible = false;
            }
            textBoxCode.Focus();
        }
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_locked)
                {
                    User finduser = new User();
                    finduser.CODE = textBoxCode.Text;
                    finduser.PASSWORD = textBoxPassword.Text;
                    if (finduser.CODE.ToLower() != m_user.CODE.ToLower())
                    {
                        MessageBox.Show("Invalid User", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        textBoxCode.Focus();
                        textBoxCode.SelectAll();
                        return;
                    }
                    if (finduser.PASSWORD != m_user.PASSWORD)
                    {
                        MessageBox.Show("Invalid User", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        textBoxCode.Focus();
                        textBoxCode.SelectAll();
                        return;
                    }
                    m_user = finduser;
                    m_mainForm.SetUser(finduser);
                    m_locked = false;
                    this.Close();
                }
                else
                {
                    if (isValid())
                    {
                        m_user = new User();
                        m_user.CODE = textBoxCode.Text;
                        User finduser = (User)RepositoryFactory.GetInstance().CodeNameRepository().GetByCode(m_user);
                        if (finduser == null)
                        {
                            MessageBox.Show("Invalid User", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            textBoxCode.Focus();
                            textBoxCode.SelectAll();
                            return;
                        }
                        if (finduser.PASSWORD != textBoxPassword.Text)
                        {
                            MessageBox.Show("Invalid User", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            textBoxCode.Focus();
                            textBoxCode.SelectAll();
                            return;
                        }
                        m_user = finduser;
                        m_mainForm.SetUser(finduser);
                        this.Close();
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void Clear()
        {
            textBoxCode.Text = "";
            textBoxPassword.Text = "";
            errorProvider1.Clear();
        }
        private bool isValid()
        {
            bool a = textBoxCode.Text == "";
            bool c = textBoxPassword.Text == "";

            if (a) errorProvider1.SetError(textBoxCode, "Code Can not Empty");
            if (c) errorProvider1.SetError(textBoxPassword, "Password Can not Empty");

            return !a && !c;
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form mainform = (Form)m_mainForm;
            if (m_locked)
            {
                if (MessageBox.Show("It will close all system, continue?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    mainform.Close();
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                mainform.Visible = true;
            }
        }
    }
}
